﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using StudentWeb.BLL;
using Newtonsoft.Json.Linq;
using StudentWeb.Common;

namespace StudentWeb.Api.Controllers
{
    public class XhUserController : ApiController
    {
        XhUserBO _xhuserBO = new XhUserBO();
        #region User用户 登录
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult Login([FromBody]JObject obj)
        {
            dynamic model = obj;
            string XhUserName = model.XhUserName ?? "";
            string XhUserPassword = model.XhUserPassword ?? "";
            if (string.IsNullOrEmpty(XhUserName) || string.IsNullOrEmpty(XhUserPassword))
            {
                return Json(new Result(ResultCode.Fail, "用户名或密码不能为空"));
            }
            try
            {

                var result = _xhuserBO.Login(XhUserName, XhUserPassword);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion


        #region User注册 注册
        /// <summary>
        /// 教师注册 注册
        /// </summary>
        /// <param name="token"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        //[ValidToken]
        public IHttpActionResult RegisterTeacherInfo(string token, [FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                var result = _xhuserBO.SaveXhUserInfo(model, 0);
                return result ? Json(new Result()) : Json(new Result(ResultCode.Fail, "保存失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
    }

}